package com.boss.salary.parsingExcel.impl;

import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.boss.asset.service.IAssetInfoService;
import com.boss.salary.ExcelModel.ExcelResult;
import com.boss.salary.ExcelModel.PersonBankModel;
import com.boss.salary.constant.ApiElementConstant;
import com.boss.salary.entity.PersonOperate;
import com.boss.salary.parsingExcel.ExcelParsingService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <br>Title:
 * <br>Description:
 * <br>Author:
 * <br>Date:
 */
@Service("personBankExcelParsingServiceImpl")
public class PersonBankExcelParsingServiceImpl implements ExcelParsingService {

    @Autowired
    private IAssetInfoService assetInfoService;

    @Override
    public <E> ExcelResult<E> readExcelWithModel(InputStream inputStream, Class<? extends BaseRowModel> clazz, ExcelTypeEnum excelTypeEnum, UserDTO userDto) {
        // 工资卡开户银行
        List<Map<String, String>> salbankList = assetInfoService.getBaseDataSelect(ApiElementConstant.SALBANK, userDto.getProvince());
        // 调用listener
        PersonBankExcelParsingListener listener = new PersonBankExcelParsingListener(salbankList);
        ExcelReader excelReader = new ExcelReader(inputStream, excelTypeEnum, null, listener);
        //默认只有一列表头
        excelReader.read(new Sheet(1, 1, clazz));
        List<PersonOperate> list = listener.getSuccessList();
        List<List<String>> errorList = listener.getErrorAllList();
        ExcelResult<PersonOperate> excelResult = new ExcelResult();
        excelResult.setSuccessList(list);
        excelResult.setErrorList(errorList);
        return (ExcelResult<E>) excelResult;
    }

    /**
     * excel解析
     * @param
     */
    private class PersonBankExcelParsingListener extends AnalysisEventListener<PersonBankModel> {

        // 工资卡开户银行
        private List<Map<String, String>> salbankList = null;

        public PersonBankExcelParsingListener(List<Map<String, String>> salbankList) {
            this.salbankList = salbankList;

        }

        /** 成功list **/
        private List<PersonOperate> successList = new ArrayList<>();
        /** 失败list **/
        private List<List<String>> errorAllList = new ArrayList<>();

        @Override
        public void invoke(PersonBankModel object, AnalysisContext analysisContext) {
            PersonOperate personOperate = new PersonOperate();
            List<String> errorList = new ArrayList<>(10);
            String errorMsg = "第"+analysisContext.getCurrentRowNum()+"行， %s值错误";
            // 姓名 必填
            Object personName = object.getPersonName();
            if (!StringUtils.isEmpty(personName)) {
                personOperate.setPersonName(personName.toString());
            } else {
                errorList.add(String.format(errorMsg, "姓名"));
            }
            // 证件号码 必填
            Object idenNo = object.getIdenNo();
            if (!StringUtils.isEmpty(idenNo)) {
                personOperate.setIdenNo(idenNo.toString());
            } else {
                errorList.add(String.format(errorMsg, "证件号码"));
            }
            // 工资卡开户银行 必填
            Object salaCardBank = object.getSalaCardBank();
            if (!StringUtils.isEmpty(salaCardBank)) {
                // 判断基础数据中是否包含读取到的值
                String code = getCodeByName(salbankList, salaCardBank.toString());
                if (!StringUtils.isEmpty(code)) {
                    personOperate.setSalaCardBank(code);
                } else {
                    errorList.add(String.format(errorMsg, "工资卡开户银行"));
                }
            } else {
                errorList.add(String.format(errorMsg, "工资卡开户银行"));
            }
            // 工资卡卡号
            Object salaCardNo = object.getSalaCardNo();
            if (!StringUtils.isEmpty(salaCardNo)) {
                personOperate.setSalaCardNo(salaCardNo.toString());
            } else {
                errorList.add(String.format(errorMsg, "工资卡卡号"));
            }
            // 如果没有错误，成功列表中增加数据
            if (errorList.size() == 0) {
                successList.add(personOperate);
            } else {
                errorAllList.add(errorList);
            }
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        }

        public List<PersonOperate> getSuccessList() {
            return successList;
        }

        public void setSuccessList(List<PersonOperate> successList) {
            this.successList = successList;
        }

        public List<List<String>> getErrorAllList() {
            return errorAllList;
        }

        public void setErrorList(List<List<String>> errorAllList) {
            this.errorAllList = errorAllList;
        }
    }


    /**
     * List<Map<String, String>> 中，根据name找code
     * @param list
     * @return
     */
    private String getCodeByName(List<Map<String, String>> list, String name) {
        String code = "";
        for (Map<String, String> map: list) {
            if (name.equals(map.get("name"))) {
                code = map.get("code");
                break;
            }
        }
        return code;
    }
}
